package com.heima.kafka.Controller;

/**
 * @Author yuan
 * @Date 2023/1/31
 * @ApiNote
 */

import com.alibaba.fastjson.JSON;
import com.heima.kafka.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 生产者发送消息
 */
@RestController
public class HelloController {

    @Autowired
    private KafkaTemplate<String, String> kafkaTemplate;

    @GetMapping("/hello")
    public String hello() {
       //kafkaTemplate.send("itcast-topic", "kafka 生产者发送消息");

        // 传递消息为对象
        User user = new User();
        user.setUserName("Rebecca");
        user.setAge(6);
        kafkaTemplate.send("user-topic", JSON.toJSONString(user));

       return "ok";
    }
}
